package com.hengxinguotong.hxgtwygsp.ui.main.picture;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.view.View;

import com.hengxinguotong.hxgtwygsp.R;
import com.hengxinguotong.hxgtwygsp.animator.TranslationBuilder;
import com.hengxinguotong.hxgtwygsp.model.PageEvent;
import com.hengxinguotong.hxgtwygsp.model.WeatherBean;
import com.hengxinguotong.hxgtwygsp.net.HttpObserver;
import com.hengxinguotong.hxgtwygsp.net.HttpRequest;
import com.hengxinguotong.hxgtwygsp.utils.TimeDown;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.android.FragmentEvent;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * TODO
 *
 * @author hjz
 * @version V1.0.0
 * @since 2019/3/23 13:58
 */

public class PicturePresenter implements PictureContract.IPresenter
{
    private Context context;
    private PictureContract.IView mView;

    private ObjectAnimator dismissAnimator;
    private ObjectAnimator showAnimator;

    private TimeDown pageTimeDown;
    private TimeDown handerTimeDown;
    private boolean firstInto = true;

    private HttpRequest mHttpRequest;

    @Inject
    public PicturePresenter(Context context, HttpRequest httpRequest)
    {
        this.context = context;
        this.mHttpRequest = httpRequest;
    }

    @Override
    public void bindView(PictureContract.IView iView)
    {
        this.mView = iView;
        LifecycleTransformer<Long> transformer = ((PictureFragment) mView).bindUntilEvent(FragmentEvent.DESTROY_VIEW);
        pageTimeDown = new TimeDown(transformer, () ->
        {
            mView.updateCompleted();
            EventBus.getDefault().post(new PageEvent());
        }, 20);
        handerTimeDown = new TimeDown(transformer, () ->
        {
            mView.updateHanderStatus();
            dismissAnimator();
        });
    }

    @Override
    public void dropView()
    {
        this.mView = null;
    }

    @Override
    public void start()
    {
        if (pageTimeDown != null)
            pageTimeDown.startTimer();
        if (handerTimeDown != null && firstInto)
        {
            firstInto = false;
            handerTimeDown.startTimer();
        }
    }

    @Override
    public void stop()
    {
        if (pageTimeDown != null)
            pageTimeDown.stop();
    }

    @Override
    public void initAnimator(View target)
    {
        float value = context.getResources().getDimensionPixelSize(R.dimen.px_182);
        dismissAnimator = new TranslationBuilder()
                .buildTarget(target)
                .buildPropertyName("translationX")
                .buildValues(0f, value)
                .buildDuration(400)
                .create();
        showAnimator = new TranslationBuilder()
                .buildTarget(target)
                .buildPropertyName("translationX")
                .buildValues(value, 0f)
                .buildDuration(400)
                .create();
    }

    @Override
    public void dismissAnimator()
    {
        dismissAnimator.start();
    }

    @Override
    public void showAnimator()
    {
        showAnimator.start();
        handerTimeDown.startTimer();
    }

    @Override
    public void recoverRemainTime()
    {
        handerTimeDown.recoverRemainTime();
        pageTimeDown.recoverRemainTime();
    }

    @Override
    public void getWeather() {
        mHttpRequest.getWeather()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<WeatherBean>() {
                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onSuccess(WeatherBean weatherBean) {
                        mView.getWeatherSucc(weatherBean);
                    }

                    @Override
                    public void onFail(Throwable e) {
                    }

                    @Override
                    public void onEnd() {

                    }
                });
    }
}
